package com.zhumo.sz.mock;

import java.security.SecureRandom;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 用于生成日期
 */
public class RandomGenerator {

    /**
     * 生成指定年份的日期
     */
    public static String generateRandomDate(int start,int end) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, getRandomYear(start,end));
        calendar.set(Calendar.DAY_OF_YEAR, getRandomDayInYear(calendar.get(Calendar.YEAR)));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(calendar.getTime());
    }

    public static int getRandomYear(int start,int end) {
        return ThreadLocalRandom.current().nextInt(start, end);
    }

    public static int getRandomDayInYear(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        int dayOfYear = calendar.getActualMaximum(Calendar.DAY_OF_YEAR);
        return ThreadLocalRandom.current().nextInt(1, dayOfYear + 1);
    }

    /**
     * 生成随机数
     */
    public static String generateRandomNumber(int numberOfDigits) {
        if (numberOfDigits <= 0) {
            throw new IllegalArgumentException("位数必须大于0");
        }
        Random random = new Random();
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < numberOfDigits; i++) {
            int digit = random.nextInt(10); // 0到9之间的随机数字
            sb.append(digit);
        }

        return sb.toString();
    }

    /**
     * 生成指定范围内的随机数
     */
    public static int generateAppointNumber(int min, int max) {
        if (min >= max) {
            throw new IllegalArgumentException("最小值必须小于最大值");
        }
        Random random = new Random();
        return random.nextInt(max - min + 1) + min;
    }

    /**
     * 生成指定范围内的时间戳
     */
    public static String generateRandomTimeStamp(String dateString) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf.parse(dateString);
            // 设置5:50到24:00之间随机生成时间
            long startTimestamp = date.getTime() + (5 * 60 + 50) * 60 * 1000; // 5:50的时间戳
            long endTimestamp = date.getTime() + 24 * 60 * 60 * 1000; // 24:00的时间戳
            long timestamp = startTimestamp + (long) (Math.random() * (endTimestamp - startTimestamp)); // 在5:50到24:00之间随机生成时间戳
            Date randomDate = new Date(timestamp);
            int hour = randomDate.getHours();
            int minute = randomDate.getMinutes();
            Random random = new Random();
            double probability = random.nextDouble();
            // 根据概率生成时间
            if ((hour < 10 && probability < 0.75) || (hour >= 18 && probability < 0.75)) {
                return formatDate(randomDate);
            } else {
                if (!(hour == 0 && minute <= 20)) { // 确保不在0:20之前
                    return generateRandomTimeStamp(dateString);
                }
            }
            return formatDate(randomDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return "Error occurred";
        }
    }

    /**
     * 根据结束时间倒起始时间
     * @param dateTimeString 流量结束时间
     * @param s 乘坐时间 秒为单位
     */
    public static String generateStartTime(String dateTimeString, long s){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(dateTimeString); // 解析日期时间字符串
            long timeInMillis = date.getTime(); // 获取日期时间的毫秒数
            timeInMillis -= (s * 1000); // 减去指定的秒数
            date.setTime(timeInMillis); // 设置新的时间
            return sdf.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String formatDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 生成随机字符串
     */
    public static String generateRandomString(int length) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        SecureRandom random = new SecureRandom();
        StringBuilder sb = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(characters.length());
            sb.append(characters.charAt(randomIndex));
        }

        return sb.toString();
    }

//    public static void main(String[] args) {
//        String s = generateRandomTimeStamp("2024-10-01");
//        String startTime = generateStartTime(s, 10);
//        System.out.println(s);
//        System.out.println(startTime);
//    }
}
